
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _babelHelperVueJsxMergeProps = _interopRequireDefault(require('@vue/babel-helper-vue-jsx-merge-props'));

const _utils = require('../utils');

const _functional = require('../utils/functional');

const _constant = require('../utils/constant');

const _popup = require('../mixins/popup');

const _icon = _interopRequireDefault(require('../icon'));

const _popup2 = _interopRequireDefault(require('../popup'));

const _loading = _interopRequireDefault(require('../loading'));

// Utils
// Mixins
// Components
const _createNamespace = (0, _utils.createNamespace)('action-sheet');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

function ActionSheet(h, props, slots, ctx) {
  const { title } = props;
  const { cancelText } = props;

  function onCancel() {
    (0, _functional.emit)(ctx, 'input', false);
    (0, _functional.emit)(ctx, 'cancel');
  }

  function Header() {
    if (title) {
      return h('div', {
        class: bem('header'),
      }, [title, h(_icon.default, {
        attrs: {
          name: props.closeIcon,
        },
        class: bem('close'),
        on: {
          click: onCancel,
        },
      })]);
    }
  }

  function Content() {
    if (slots.default) {
      return h('div', {
        class: bem('content'),
      }, [slots.default()]);
    }
  }

  function Option(item, index) {
    const { disabled } = item;
    const { loading } = item;
    const { callback } = item;

    function onClickOption(event) {
      event.stopPropagation();

      if (disabled || loading) {
        return;
      }

      if (callback) {
        callback(item);
      }

      (0, _functional.emit)(ctx, 'select', item, index);

      if (props.closeOnClickAction) {
        (0, _functional.emit)(ctx, 'input', false);
      }
    }

    function OptionContent() {
      if (loading) {
        return h(_loading.default, {
          attrs: {
            size: '20px',
          },
        });
      }

      return [h('span', {
        class: bem('name'),
      }, [item.name]), item.subname && h('span', {
        class: bem('subname'),
      }, [item.subname])];
    }

    return h('button', {
      attrs: {
        type: 'button',
      },
      class: [bem('item', {
        disabled,
        loading,
      }), item.className, _constant.BORDER_TOP],
      style: {
        color: item.color,
      },
      on: {
        click: onClickOption,
      },
    }, [OptionContent()]);
  }

  function CancelText() {
    if (cancelText) {
      return h('button', {
        attrs: {
          type: 'button',
        },
        class: bem('cancel'),
        on: {
          click: onCancel,
        },
      }, [cancelText]);
    }
  }

  const Description = props.description && h('div', {
    class: bem('description'),
  }, [props.description]);
  return h(_popup2.default, (0, _babelHelperVueJsxMergeProps.default)([{
    class: bem(),
    attrs: {
      position: 'bottom',
      round: props.round,
      value: props.value,
      overlay: props.overlay,
      duration: props.duration,
      lazyRender: props.lazyRender,
      lockScroll: props.lockScroll,
      getContainer: props.getContainer,
      closeOnPopstate: props.closeOnPopstate,
      closeOnClickOverlay: props.closeOnClickOverlay,
      safeAreaInsetBottom: props.safeAreaInsetBottom,
    },
  }, (0, _functional.inherit)(ctx, true)]), [Header(), Description, props.actions && props.actions.map(Option), Content(), CancelText()]);
}

ActionSheet.props = (0, _extends2.default)({}, _popup.popupMixinProps, {
  title: String,
  actions: Array,
  duration: [Number, String],
  cancelText: String,
  description: String,
  getContainer: [String, Function],
  closeOnPopstate: Boolean,
  closeOnClickAction: Boolean,
  round: {
    type: Boolean,
    default: true,
  },
  closeIcon: {
    type: String,
    default: 'cross',
  },
  safeAreaInsetBottom: {
    type: Boolean,
    default: true,
  },
  overlay: {
    type: Boolean,
    default: true,
  },
  closeOnClickOverlay: {
    type: Boolean,
    default: true,
  },
});

const _default = createComponent(ActionSheet);

exports.default = _default;
